package com.example.exam.ExcelListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.CellExtra;
import com.alibaba.excel.read.listener.ReadListener;
import com.example.exam.entity.temp.TempStudent;
import com.example.exam.service.admin.AdminService;
import com.example.exam.utils.JsonUtils;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhang
 */
@Data
@Log4j2
@Component
public class AddStudentListener implements ReadListener<TempStudent> {

    private List<TempStudent> tempStudentList;
    JsonUtils<TempStudent> jsonUtils;
    private AdminService adminService;
    private Integer BATCH_COUNT = 10;
    private List<String> tableName = new ArrayList<>();
    private List<TempStudent> failUser = new ArrayList<>();

    @Autowired
    public AddStudentListener(AdminService adminService) {
        this.adminService = adminService;
        this.tempStudentList = new ArrayList<>(BATCH_COUNT);
        this.jsonUtils = new JsonUtils<>();
        tableName.add("name");
        tableName.add("sex");
        tableName.add("age");
        tableName.add("idcard");
    }

    @Override
    public void onException(Exception e, AnalysisContext analysisContext) throws Exception {
        throw e;
    }

    @Override
    public void invokeHead(Map<Integer, CellData> map, AnalysisContext analysisContext) {
        map.forEach((k, v) -> {
            int i = tableName.indexOf(v.getStringValue());
            log.info("i{}", i);
            log.info("head:{}", v);
            if (i != -1) {
                tableName.remove(v.getStringValue());
            }
        });
        if (!tableName.isEmpty()) {
            throw new RuntimeException("数据源不包含必要字段");
        }
    }

    @Override
    public void invoke(TempStudent TempStudent, AnalysisContext analysisContext) {
        log.info("解析到一条数据:{}", jsonUtils.t2String(TempStudent));
        tempStudentList.add(TempStudent);
        if (tempStudentList.size() >= BATCH_COUNT) {
            save();
            tempStudentList.clear();
        }

    }

    @Override
    public void extra(CellExtra cellExtra, AnalysisContext analysisContext) {

    }


    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        save();
        log.info("数据解析完成");
        if (!failUser.isEmpty()) {
            String s = JsonUtils.o2s(failUser);
            String fileName = LocalDateTime.now() + "failUser.json";
            File file = new File("F:\\" + fileName);
            FileOutputStream fileOutputStream = null;
            // 保存失败的名单
            throw new RuntimeException("添加失败的人员");
        }
    }

    @Override
    public boolean hasNext(AnalysisContext analysisContext) {
        return false;
    }

    private void save() {
        failUser.addAll(adminService.saveAllStudent(tempStudentList));
        log.info("共有{}条待保存数据", tempStudentList.size());
        log.info("保存成功");
    }
}
